#include <QCoreApplication>
#include<QDir>
#include<QStringList>
#include<QDebug>


//只定义函数实现获取目录下大小

qint64 GetDirFileInfoSizeFunc(const QString &qpath)
{
    //QDir类专门用来操作路径名称
    QDir path(qpath);
    //遍历取出非文件大小

    qint64 size;//存放文件大小
    //entryInfoList(const QStringList &nameFilters, QDir::Filters filters = NoFilter, QDir::SortFlags sort = NoSort) const
    foreach(QFileInfo info,path.entryInfoList(QDir::Files))
    {
        size=size+info.size();
    }

    //QDir::Dirs列出文件目录 QDir::separator()为当前文件的目录如 ‘/’

    foreach(QString list,path.entryList(QDir::Dirs|QDir::NoDotAndDotDot))
    {
        size=size+GetDirFileInfoSizeFunc(qpath+QDir::separator()+list);

    }
    char unit='B';
    qint64 currentSize=size;

    if(currentSize>1024)
    {
        currentSize=currentSize/1024;
        unit='K';
        if(currentSize>1024)
        {
            currentSize=currentSize/1024;
            unit='M';
            if(currentSize>1024)
            {
                currentSize=currentSize/1024;
                unit='G';
                if(currentSize>1024)
                {
                    currentSize=currentSize/1024;
                    unit='T';
                }

            }
        }
    }
    qDebug()<<"目录占据空间为:"<<currentSize<<unit<<"\t"<<qPrintable(qpath);

    return size;
}
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    //该字符串存储路径

    QString strpath;

    strpath=QDir::currentPath();//获取当前目录
    qDebug()<<"当前的目录为："<<strpath<<endl;

    //调用此函数求目录占据空间的大小

    GetDirFileInfoSizeFunc(strpath);


    return a.exec();
}
